﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.Course;

public partial class sysadmin_DL_Project_AddClass : PowerCreator.LMS.Web.Base.WebBasePage
{
    TOCourseList objCourseList = new TOCourseList();
    VOTOCourseList voCourseList = new VOTOCourseList();
    
    TOClass objClass = new TOClass();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    PowerCreator.LMS.ViewQuery.ViewQuery opViewQuery = new PowerCreator.LMS.ViewQuery.ViewQuery();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            BinderCourseList();
            BinderSchoolStage(int.Parse(ddl_CourseList.SelectedValue));
            BinderCatalog(int.Parse(ddl_CourseList.SelectedValue));

            //BinderTeacher();
            //BinderClassAssistant();
            //BinderClassLeader();
            //BinderClassLecturer();

            int CID = 0;
            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                ViewState["CID"] = CID.ToString();
                BinderForm(CID);
            }

        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 3)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }

    }
    private int CheckPopedom()
    {
        int rt = 0;

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            rt = login.VOAdvancedUser.AUserGroup;
        }
        return rt;

    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        //lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_role.Text = System.Configuration.ConfigurationManager.AppSettings["ClassNameRole"].ToString();

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }


   


    //绑定课程资源分类
    private void BinderCourseList()
    {
        TOCourseList obj = new TOCourseList();
        string search = " Used=1";
        DataTable dt = obj.Query(search);

        ddl_CourseList.DataSource = dt;
        ddl_CourseList.DataTextField = "CourseListName";
        ddl_CourseList.DataValueField = "CourseList_Id";
        ddl_CourseList.DataBind();

    }



    //绑定学段
    private void BinderSchoolStage(int CourseList_Id)
    {
        TOSchoolStage obj = new TOSchoolStage();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_XueDuan.DataSource = dt;
        ddl_XueDuan.DataTextField = "SchoolStage";
        ddl_XueDuan.DataValueField = "ID";
        ddl_XueDuan.DataBind();

        voCourseList = objCourseList.Load(CourseList_Id);
        ddl_XueDuan.SelectedValue = voCourseList.Xueduan.ToString();
        ddl_XueDuan.Enabled = false;
        bandtime(ddl_CourseList.SelectedValue);
    }

    //绑定学科
    private void BinderCatalog(int CourseList_Id)
    {
        Catalog obj = new Catalog();
        string search = "";
        DataTable dt = obj.QueryCatalog(search);

        ddl_XueKe.DataSource = dt;
        ddl_XueKe.DataTextField = "CatalogName";
        ddl_XueKe.DataValueField = "CatalogId";
        ddl_XueKe.DataBind();

        voCourseList = objCourseList.Load(CourseList_Id);
        ddl_XueKe.SelectedValue = voCourseList.Xueke.ToString();
        ddl_XueKe.Enabled = false;

    }

    ////绑定教师
    //private void BinderTeacher()
    //{
    //    TOAdvancedUser obj = new TOAdvancedUser();
    //    string search = " ";
    //    DataTable dt = obj.Query(search);

    //    ddl_ClassAssistant.DataSource = dt;
    //    ddl_ClassAssistant.DataTextField = "Name";
    //    ddl_ClassAssistant.DataValueField = "AUser_Id";
    //    ddl_ClassAssistant.DataBind();

    //    ddl_ClassLeader.DataSource = dt;
    //    ddl_ClassLeader.DataTextField = "Name";
    //    ddl_ClassLeader.DataValueField = "AUser_Id";
    //    ddl_ClassLeader.DataBind();


    //    ddl_ClassLecturer.DataSource = dt;
    //    ddl_ClassLecturer.DataTextField = "Name";
    //    ddl_ClassLecturer.DataValueField = "AUser_Id";
    //    ddl_ClassLecturer.DataBind();


    //}
    ////绑定教师
    //private void BinderClassLeader()
    //{
    //    TOCourseList obj = new TOCourseList();
    //    string search = " Used=1";
    //    DataTable dt = obj.Query(search);

    //    ddl_CourseList.DataSource = dt;
    //    ddl_CourseList.DataTextField = "CourseListName";
    //    ddl_CourseList.DataValueField = "CourseList_Id";
    //    ddl_CourseList.DataBind();

    //}
    ////绑定教师
    //private void BinderClassLecturer()
    //{
    //    TOCourseList obj = new TOCourseList();
    //    string search = " Used=1";
    //    DataTable dt = obj.Query(search);

    //    ddl_CourseList.DataSource = dt;
    //    ddl_CourseList.DataTextField = "CourseListName";
    //    ddl_CourseList.DataValueField = "CourseList_Id";
    //    ddl_CourseList.DataBind();

    //}



    private void BinderForm(int id)
    {
        VOTOClass vo = new VOTOClass();
        vo = objClass.Load(id);

        txt_Name.Text = vo.Name;
        txt_Introduce.Text = vo.Description;
        ddl_CourseList.SelectedValue = vo.ClassCourseListID.ToString();
        ddl_XueDuan.SelectedValue = vo.XueDuan.ToString();
        ddl_XueKe.SelectedValue = vo.XueKe.ToString();
        txt_endtime.Text = vo.EndTime.ToString().Trim();
        //ddl_ClassLecturer.SelectedValue = vo.ClassLecturer.ToString();
        //ddl_ClassLeader.SelectedValue = vo.ClassLeader.ToString();
        //ddl_ClassAssistant.SelectedValue = vo.ClassAssistant.ToString();
        if (vo.Broswer)
        {
            radbtn_Yes.Checked = true;
            radbtn_No.Checked = false;
        }
        else
        {
            radbtn_Yes.Checked = false;
            radbtn_No.Checked = true;
        }

        if (CheckPopedom() == 3)
        {
            txt_Name.Enabled = false;
            txt_Introduce.Enabled = false;
            ddl_CourseList.Enabled = false;
            //ddl_ClassLecturer.Enabled = false;
            radbtn_Yes.Enabled = false;
            radbtn_No.Enabled = false;
        }
        bandtime(ddl_CourseList.SelectedValue);


    }
    private bool CheckPageData()
    {
        string search = string.Format("Name = '{0}'", txt_Name.Text.Trim());
        if (ViewState["CID"] != null)
        {
            search += "and Class_Id<>" + ViewState["CID"].ToString();
        }
        if (objClass.Count(search) != 0)
        { 
            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        if (string.IsNullOrEmpty(txt_endtime.Text))
        {
            lbl_msg_Error.Text = "<br />必须选择结束时间！";
            return false;
        }
        if (!string.IsNullOrEmpty(txt_endtime.Text.Trim()))
        {
            string str_search = "select * from TO_Batch where Batch_Id=(select Batch_id from TO_CourseList where CourseList_Id={0}) and EndDate>='{1}'";
            string str_courselist = ddl_CourseList.SelectedValue.Trim();
            string str_endtime = txt_endtime.Text;
            string search_endtime = string.Format(str_search,str_courselist,str_endtime);
            if (opViewQuery.Count(search_endtime) <= 0)
            {
                lbl_msg_Error.Text = "<br />结束时间不能大于所选批次的结束时间！";
                return false;
            }
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOClass vo = new VOTOClass();
        if (ViewState["CID"] == null)
        {
            vo.Name = txt_Name.Text.Trim();
            vo.Description = txt_Introduce.Text.Trim();
            vo.ClassCourseListID = int.Parse(ddl_CourseList.SelectedValue);
            vo.XueKe = int.Parse(ddl_XueKe.SelectedValue);
            vo.XueDuan = int.Parse(ddl_XueDuan.SelectedValue);

            //vo.ClassLecturer = int.Parse(ddl_ClassLecturer.SelectedValue);
            //vo.ClassLeader = int.Parse(ddl_ClassLeader.SelectedValue);
            //vo.ClassAssistant = int.Parse(ddl_ClassAssistant.SelectedValue);
            vo.EndTime = Convert.ToDateTime(txt_endtime.Text.Trim());
            if (radbtn_Yes.Checked == true)
                vo.Broswer = true;
            if (radbtn_No.Checked == true)
                vo.Broswer = false;
            vo.CreatDate=DateTime.Now;
             PowerCreator.LMS.Web.Base.BasePage bpage=new PowerCreator.LMS.Web.Base.BasePage();
             vo.Creator = bpage.GetCreatedByInfo().UserID;
             vo.CreatorName = bpage.GetCreatedByInfo().UserName;
             
            if (objClass.Add(vo))
            {
                string url = "ManageClass.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            vo = objClass.Load(int.Parse(ViewState["CID"].ToString()));
            vo.Name = txt_Name.Text.Trim();
            vo.Description = txt_Introduce.Text.Trim();

            vo.ClassCourseListID = int.Parse(ddl_CourseList.SelectedValue);
            vo.XueKe = int.Parse(ddl_XueKe.SelectedValue);
            vo.XueDuan = int.Parse(ddl_XueDuan.SelectedValue);

            //vo.ClassLecturer = int.Parse(ddl_ClassLecturer.SelectedValue);
            //vo.ClassLeader = int.Parse(ddl_ClassLeader.SelectedValue);
            //vo.ClassAssistant = int.Parse(ddl_ClassAssistant.SelectedValue);
            vo.EndTime = Convert.ToDateTime(txt_endtime.Text.Trim());

            if (radbtn_Yes.Checked == true)
                vo.Broswer = true;
            if (radbtn_No.Checked == true)
                vo.Broswer = false;
            vo.CreatDate = DateTime.Now;
            PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
            vo.Creator = bpage.GetCreatedByInfo().UserID;
            
            if (objClass.Update(vo))
            {
                string url = "ManageClass.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageClass.aspx";
        Script.RedirectURL(url);
    }


    protected void ddl_CourseList_SelectedIndexChanged(object sender, EventArgs e)
    {
        bandtime(ddl_CourseList.SelectedValue);
        BinderSchoolStage(int.Parse(ddl_CourseList.SelectedValue));
        BinderCatalog(int.Parse(ddl_CourseList.SelectedValue));

    }

    protected void bandtime(string str_courslistid)
    {
        string str_searc = string.Format("select * from TO_Batch where Batch_Id =(select Batch_Id from TO_CourseList where CourseList_Id={0})", str_courslistid);
        DataTable dt_time = opViewQuery.Query(str_searc);
        string str_staData = DateTime.Now.ToString("yyy-MM-dd"); 
        string str_endData = DateTime.Now.ToString("yyy-MM-dd"); 
        if(dt_time.Rows.Count>0)
        {
            str_staData=dt_time.Rows[0]["StartDate"].ToString().Trim();
            str_endData = dt_time.Rows[0]["EndDate"].ToString().Trim();
        }

        lbl_Time.Text = "(" + str_staData + "---" + str_endData + ")";
    }
}